package com.ag.zhh.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ag.dao.TbContactDAO;
import com.ag.zhh.vo.Contact;

public class ContactDAO extends HibernateDaoSupport {
	
	private static final Log log = LogFactory.getLog(ContactDAO.class);
	
	public void saveOrUpdateContact(Contact contact) {
		log.debug("saveOrUpdateContact contact!");
		this.getHibernateTemplate().saveOrUpdate(contact);
	}
	
	public void deleteContact(Contact contact) {
		log.debug("deleteContact contact!");
		this.getHibernateTemplate().delete(contact);
	}
	/**
	 * 根据OA通信录的id查询
	 * @param CId
	 * @return
	 */
	public Contact findById(String CId) {
		log.debug("findContact byCId!");
		return (Contact)this.getHibernateTemplate().get(Contact.class, CId);
		
	}
	/**
	 * 根据OA部门id查询通信录
	 * @param deptId
	 * @return
	 */
	public List<Contact> findByDeptId(String deptId)
	{
		List<Contact> result=null;
		Session session = this.getSessionFactory().openSession();
		result = session.createSQLQuery("select * from TB_CONTACT contact left outer join TB_DEPT_AND_CONTACT dept_contact on contact.C_ID = dept_contact.C_ID where dept_contact.D_ID = :did").addEntity(Contact.class).setString("did", deptId).list();
		if(result!=null)
		System.out.println("ContactDAO： OA部门："+deptId+" 通信录数目："+result.size());
		
		session.close();
		return result;
		
	}
	/**
	 * 查询所有的OA通信录
	 * @return
	 */
	public List<Contact> findAll()
	{
		List<Contact> result=null;
		Session session = this.getSessionFactory().openSession();
		result = session.createQuery("from Contact").list();
		session.close();
		return null;
		
	}
	//根据联系人姓名查找联系人
	public List<Contact> findContactByName(String name)
	{
		log.debug("findContact byCName!");
		return getHibernateTemplate().findByNamedParam("from Contact c where c.CName like :cname", "cname","%"+name+"%");
	}
}

